<template>
  <!-- 导航栏组件 -->
  <div id="nav-bar">
    <div class="nav-box">
      <div
        :class="{ active: nav.id == navIndex }"
        class="item"
        v-for="nav in navs"
        :key="nav.id"
        @click="navTo(nav.id)"
      >
        <span>{{ nav.label }}</span>
        <div class="bar"></div>
      </div>
    </div>
    <div class="show">
      <transition name="user-router" mode="out-in">
        <router-view></router-view>
      </transition>
    </div>
  </div>
</template>

<script>
export default {
  name: "NavBar",
  props: ["navs"],
  data() {
    return {
      navIndex: 0,
    };
  },
  methods: {
    navTo(id) {
      this.$router.push(this.navs[id].url);
      this.navIndex = id;
    },
  },
  updated() {
    // 初始化navIndex
    // 获取路由信息
    let { path } = this.$route;
    this.navs.forEach((item) => {
      if (path.indexOf(item.type) != -1) {
        this.navIndex = item.id;
      }
    });
  },
  mounted() {
    // 初始化navIndex
    // 获取路由信息
    let { path } = this.$route;
    this.navs.forEach((item) => {
      if (path.indexOf(item.type) != -1) {
        this.navIndex = item.id;
      }
    });
  },
};
</script>

<style lang='less'>
// 动画
.user-router-enter,
.user-router-leave-to {
  transform: translateY(50vh);
}
.user-router-enter-active,
.user-router-leave-active {
  transition: all 0.4s ease;
}
#nav-bar {
  .nav-box {
    margin-top: 30px;
    display: flex;
    justify-content: center;
    overflow-x: scroll;
    height: 30px;
    padding: 0 60px;
    .item {
      cursor: pointer;
      margin-right: 30px;
      position: relative;
      height: 25px;
      white-space: nowrap;
      span {
        font-size: 16px;
      }
      .bar {
        position: absolute;
        display: none;
        bottom: -3px;
        height: 3px;
        width: 100%;
        background: var(--themeColor1);
      }
    }
    .active {
      span {
        font-weight: bold;
      }
      .bar {
        display: block;
      }
    }
  }
  .show {
    margin-top: 20px;
  }
}
</style>